<?php
    require_once('paths.inc');
    require_once('page.inc');
    require_once(INCLUDE_PATH.'constants.inc');
    require_once(DB_PATH.'db.inc');
    require_once('security.inc');
    require_once('utils.inc');
    
    session_start();    
    checkIsLoggedIn();
    
	function show_interface () {
		$db = db_connect();
		select_language();

		if (isset($_GET['index'])) 
            $index = $_GET['index'];
		
		if (isset($index)) {
			$text = LANG_USER_UPDATE;
            $user = $_SESSION['users'][$index];
			$opt = "update";
		}
		else {
			$text = LANG_USER_CONFIRM;
			$opt = "insert";
			$user = new User(null,null,null,null,null);
		}
        
        $help_text = LANG_EXTRA_MODAL_ADD_USER;
        if ($opt == "update")
            $help_text = LANG_EXTRA_MODAL_EDIT_USER;
		
		$ret = "";
//		var_dump($_SESSION['users'][$index]);
		if (!isset($index)) 
            $index = 0;
			$ret .= "<div align=\"center\">";	
				$ret .= '<table id="wrapper2">';
					$ret .= '<thead>';	
						$ret .= "<tr>";
							$ret .= "<th colspan=\"4\" align=\"center\">" . $text . "</th>";
						$ret .= "</tr>";
					$ret .= '</thead>';
					$ret .= '<tr class="capTabel">';
						$ret .= '<td id="td_username">'.LANG_Username . CT_OBLIGATORIU . '</td>';
						$ret .= '<td align="left"><input type="text" value="' . $user->username . '" name="username" id="username" />';
                        $ret .= '<td id="td_email">'.LANG_EMAIL . CT_OBLIGATORIU . '</td>';
                        $ret .= '<td align="left"><input type="text" value="' . $user->email . '" name="email" id="email" />';
					$ret .= '</tr>';
					$ret .= '<tr class="capTabel">';
						$ret .= '<td id="td_nume">'.LANG_NUME . CT_OBLIGATORIU . '</td>';
						$ret .= '<td align="left"><input type="text" value="' . $user->nume . '" name="nume" id="nume" />';
                        $ret .= '<td id="td_prenume">'.LANG_PRENUME . CT_OBLIGATORIU . '</td>';
                        $ret .= '<td align="left"><input type="text" value="' . $user->prenume . '" name="prenume" id="prenume" />';
					$ret .= '</tr>';
					
                    $ret .= '<tr class="capTabel">';
                        $ret .= '<td colspan="2" align="left"><strong>'.LANG_Permisiuni_user.'</strong></td>';
                        $ret .= '<td colspan="2" align="left"><a onclick="javascript: select_all_permissions(true); " style="cap_tabel">' . LANG_Select_all . '</a> / <a onclick="javascript: select_all_permissions(false); " style="cap_tabel">' . LANG_Deselect_all . '</a></td>';
                    $ret .= '</tr>';
                    $ret .= '<tr class="capTabel">';                                  
                        if (($user->tip & CT_PERM_COMENZI) == CT_PERM_COMENZI)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_comenzi" id="perm_comenzi" '.$option.'/>';
                        $ret .= '<label for="perm_comenzi" id="td_perm_comenzi">'.LANG_PERM_COMENZI.'</label></td>';
                        if (($user->tip & CT_PERM_COMENZI_DUPA_CONFIRMARE) == CT_PERM_COMENZI_DUPA_CONFIRMARE)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_comenzi_dupa_confirmare" id="perm_comenzi_dupa_confirmare" '.$option.'/>';
                        $ret .= '<label for="perm_comenzi_dupa_confirmare" id="td_perm_comenzi_dupa_confirmare">'.LANG_PERM_COMENZI_DUPA_CONFIRMARE.'</label></td>';
                            
                    $ret .= '</tr>';
                    $ret .= '<tr class="capTabel">';
                        if (($user->tip & CT_PERM_FIRME) == CT_PERM_FIRME)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_firme" id="perm_firme" '.$option.'/>';
                        $ret .= '<label for="perm_firme" id="td_perm_firme">'.LANG_PERM_FIRME.'</label></td>';
                        
                        if (($user->tip & CT_PERM_EDIT_USERS) == CT_PERM_EDIT_USERS)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_edit_users" id="perm_edit_users" '.$option.'/>';
                        $ret .= '<label for="perm_edit_users" id="td_perm_edit_users">'.LANG_PERM_EDIT_USERS.'</label></td>';
                    $ret .= '</tr>';
                    $ret .= '<tr class="capTabel">';
                        if (($user->tip & CT_PERM_PLATI) == CT_PERM_PLATI)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_plati" id="perm_plati" '.$option.'/>';
                        $ret .= '<label for="perm_plati" id="td_perm_plati">'.LANG_PERM_PLATI.'</label></td>';
                        
                        if (($user->tip & CT_PERM_STATISTICI) == CT_PERM_STATISTICI)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_statistici" id="perm_statistici" '.$option.'/>';
                        $ret .= '<label for="perm_statistici" id="td_perm_statistici">' . LANG_PERM_STATISTICI . '</label></td>';
                    $ret .= '</tr>';
                    $ret .= '<tr class="capTabel">';
                        if (($user->tip & CT_PERM_EDIT_CLIENT) == CT_PERM_EDIT_CLIENT)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_edit_clienti" id="perm_edit_clienti" '.$option.'/>';
                        $ret .= '<label for="perm_edit_clienti" id="td_perm_edit_clienti">'.LANG_PERM_EDIT_CLIENT.'</label></td>';
                        
					    if (($user->tip & CT_PERM_ADMIN_ABONAMENT) == CT_PERM_ADMIN_ABONAMENT)
                            $option = ' checked="checked"';
                        else
                            $option = '';
                        $ret .= '<td colspan="2" align="left"><input type="checkbox" value="1" name="perm_admin_abonament" id="perm_admin_abonament" '.$option.'/>';
                        $ret .= '<label for="perm_admin_abonament" id="td_perm_admin_avonament">' . LANG_PERM_ADMIN_ABONAMENT . '</label></td>';
                    $ret .= '</tr>';
					$ret .= '<tr class="capTabel">';
						$ret .= '<td colspan="4" align="center">';
							$ret .= '<input type="button" name="user_confirm" id="user_confirm" value="' . $text . '"' . ' onclick="javascript: new_user_confirm(\'' . $opt .'\',' . $index . ');" />&nbsp;';
							$ret .= '<input type="button" name="cancel" value="'.LANG_CANCEL.'" onclick="javascript: closeMessage();" />';
						$ret .= "</td>";
					$ret .= '</tr>';
                    $ret .= output_extra_text_modal($help_text, 4);
				$ret .= '</table>';			
			$ret .= "</div>";
		$ret .= '<script type="text/javascript">';
			$ret .= 'document.getElementById(\'username\').focus();';
		$ret .='</script>';
		
		echo $ret;
		flush();
	}
	
	show_interface();
?>